<template>
	<view class="uni-tab-bar">
		<scroll-view id="tab-bar" class="uni-swiper-tab" scroll-x :scroll-left="scrollLeft">
			<view v-for="(tab,index) in tabBars" :key="tab.id" :class="['swiper-tab-list',tabIndex==index ? 'active' : '']" :id="tab.id"
			 :data-current="index" @click="tapTab(index)">{{tab.name}}</view>
		</scroll-view>
		<swiper :current="tabIndex" class="swiper-box" duration="300" @change="changeTab">
			<swiper-item v-for="(tab,index1) in tabBars" :key="index1">
				<scroll-view v-if="tabIndex==0" class="list" scroll-y @scrolltolower="loadMore(index1)" @scroll="scroll">
					<block v-for="(item,index2) in newsitems" :key="index2">
						<media-list :data="item" @click="goDetail(item)"></media-list>
					</block>
					<view class="shuju_con" v-if="shuju_onoff">没有数据了</view>
					<view class="shuju_con" v-if="!shuju_onoff"><view class="icon_loade"></view>数据加载中...</view>
				</scroll-view>
				
				<scroll-view v-if="tabIndex==1" class="list" scroll-y @scrolltolower="loadMore(index1)" @scroll="scroll">
					<block v-for="(item,index2) in financials" :key="index2">
						<media-list :data="item" @click="goDetail(item)"></media-list>
					</block>
					<view class="shuju_con" v-if="shuju_onoff">没有数据了</view>
					<view class="shuju_con" v-if="!shuju_onoff"><view class="icon_loade"></view>数据加载中...</view>
				</scroll-view>
			</swiper-item>
		</swiper>
		
		<!-- 搜索悬浮 -->
		<view class="sousuo iconfont icon-sousuo" @tap="search_data()"></view>
		<!--自定义弹框http://ext.dcloud.net.cn/plugin?id=134-->
		<neil-modal :show="sousuo_onoff" @close="close_input" :title="'搜索'+tabBars[tabIndex].name" @cancel="close_input" @confirm="Manual()">
		    <view class="input-view" v-if="tabIndex==0">
		        <view class="input-name">
		            <view>物料编号</view>
		            <input type="text" @blur="blur_input" v-model="search.materialNumber" :value="search.materialNumber" placeholder="请输入物料编号"/>
		        </view>
		    	<view class="input-name">
		    	    <view>库存地</view>
		    	    <input type="text" @blur="blur_input" v-model="search.stockpile" :value="search.stockpile" placeholder="请输入库存地"/>
		    	</view>
		    </view>
			<view class="input-view" v-else="">
		        <view class="input-name" >
		            <view>仓位</view>
		            <input type="text" @blur="blur_input" v-model="search.warehouse" :value="search.warehouse" placeholder="请输入仓位"/>
		        </view>
		    </view>
			
		</neil-modal>
	</view>
</template>
<script>
	import bus from "../../static/js/bus.js"
	import mediaList from '@/components/tab-nvue/mediaList.vue';
	// 自定义弹框
	import neilModal from '@/components/neil-modal/neil-modal.vue';
	export default {
		components: {
			mediaList,
			neilModal
		},
		data() {
			return {
				shuju_onoff:false,//没有数据了
				scrollLeft: 0,
				isClickChange: false,
				tabIndex: 0,
				newsitems: [],//物料库存数据
				financials:[],//仓位库存数据
				page_num_caiwu:1,//物料库存的当前页
				page_num_suoyou:1,//仓位库存当前页
				newsitems_onoff:false,//仓位库存没有数据了
				financials_onoff:false,//物料库存没有数据了
				tabBars: [ {
					name: '物料库存',
					id: 'wuliao'
				},{
					name: '仓位库存',
					id: 'cangwei'
				},],
				
				//搜索
				sousuo_onoff:false,
				search:{
					warehouse :'',//仓位
					materialNumber:'',//物料编号
					stockpile:'',//库存地
				},
			}
		},
		//下拉刷新
		onPullDownRefresh() {
			let that=this
			// this.reload_list()
			if(that.tabIndex==0){//物料
				that.newsitems=[]
				that.page_num_caiwu=1
				this.randomfn()
			}else if(that.tabIndex==1){//仓位
				that.financials=[]
				that.page_num_suoyou=1
				this.randomfn()
			}
		},
		onLoad() {
			//获取仓位库存数据
			this.randomfn()// 1当前第一页
			// #ifdef APP-PLUS
				//先启用下拉刷新
				this.GLOBAL.switchPullRefresh(true)
			// #endif
		},
		methods: {
			//点击搜索
			search_data(){
				let that=this
				that.sousuo_onoff=true;
			},
			//点击确认搜索
			Manual(){
				let that =this
				that.sousuo_onoff=false
				if(that.tabIndex==0){//物料
					that.newsitems=[]
					that.page_num_suoyou=1
				}else if(that.tabIndex==1){//仓位
					that.financials=[]
					that.page_num_gong=1
				}
				that.randomfn()
			},
			//点击查看详情
			goDetail(e) {
			},
			
			//加载更多
			loadMore(e) {
				console.log(e)
				let that=this
				if(that.tabIndex==0){//物料
					that.page_num_caiwu++
					that.randomfn()
				}else if(that.tabIndex==1){//仓位
					that.page_num_suoyou++
					that.randomfn()
				}
			},
			//初始化界面
			randomfn() {
				let that=this
				that.shuju_onoff=false
				if(that.tabIndex==0){//物料库存
					that.get_wuliao()
				}else if(that.tabIndex==1){//仓位库存
					that.get_warehouse()
				}
			},
			//获取物料库存
			get_wuliao(){
				let that=this
				let req={
					matNo:that.search.materialNumber,//物料编号
					sno:that.search.stockpile,//库存地
					page:that.page_num_caiwu,//当前页
				}
				that.GLOBAL.doGet('/rest/stock',req,function(data){
					console.log(data)
					if(data){
						if(data.list.length<10){
							that.shuju_onoff=true
						}
						uni.stopPullDownRefresh();//结束下拉刷新
						that.newsitems=that.newsitems.concat(data.list)
					}
				},function(err){
					console.log(err)
				})
			},
			//获取仓位库存
			get_warehouse(){
				let that=this
				let req={
					lgpla:that.search.warehouse,//仓位
					page:that.page_num_suoyou,//当前页
				}
				that.GLOBAL.doGet('/rest/stockByLgpla',req,function(data){
					console.log(data)
					if(data){
						if(data.list.length<10){
							that.shuju_onoff=true
						}
						uni.stopPullDownRefresh();//结束下拉刷新
						that.financials=that.financials.concat(data.list)
					}
				},function(err){
					console.log(err)
				})
			},
			scroll(e){
				// #ifdef APP-PLUS
					if(e.detail.scrollTop>10){
						//禁止下拉刷新
						this.GLOBAL.switchPullRefresh(false)
					}else{
						//启用下拉刷新
						this.GLOBAL.switchPullRefresh(true)
					}
				// #endif
			},
			async changeTab(e) {
				console.log('滑动到哪个页面',e.detail.current)
				let index = e.detail.current;
				let tabBar = await this.getElSize("tab-bar"),
					tabBarScrollLeft = tabBar.scrollLeft;
				let width = 0;

				for (let i = 0; i < index; i++) {
					let result = await this.getElSize(this.tabBars[i].id);
					width += result.width;
				}
				let winWidth = uni.getSystemInfoSync().windowWidth,
					nowElement = await this.getElSize(this.tabBars[index].id),
					nowWidth = nowElement.width;
				if (width + nowWidth - tabBarScrollLeft > winWidth) {
					this.scrollLeft = width + nowWidth - winWidth;
				}
				if (width < tabBarScrollLeft) {
					this.scrollLeft = width;
				}
				this.isClickChange = false;
				this.tabIndex = index; //一旦访问data就会出问题
				
				if(index==0){//物料库存
					if(this.newsitems.length==0){
						this.randomfn()// 1当前第一页
					}
				}else if(index==1){//仓位库存
					if(this.financials.length==0){
						this.randomfn()// 1当前第一页
					}
				}
			},
			getElSize(id) { //得到元素的size
				return new Promise((res, rej) => {
					uni.createSelectorQuery().select('#' + id).fields({
						size: true,
						scrollOffset: true
					}, (data) => {
						res(data);
					}).exec();
				});
			},
			async tapTab(index) { //点击tab-bar
				console.log('点击了哪个tab',index)
				if (this.tabIndex === index) {
					return false;
				} else {
					let tabBar = await this.getElSize("tab-bar"),
						tabBarScrollLeft = tabBar.scrollLeft; //点击的时候记录并设置scrollLeft
					this.scrollLeft = tabBarScrollLeft;
					this.isClickChange = true;
					this.tabIndex = index;
				}
			},
			//关闭弹框
			close_input(){
				this.sousuo_onoff=false//关闭搜素弹框
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				});
			},
			//失去焦点或收起软键盘
			blur_input(){
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				});
			},
			
		}
	}
</script>

<style scoped="scoped" lang="scss">
	@import "./warehouseInventory_page.scss";
	/* #ifdef MP-ALIPAY */
	.swiper-tab-list {
		display: inline-block;
	}
	/* #endif */
</style>
